import request from '@/common/request';
export function convertToDict(array, keyField, valueField) {
  return array.reduce((acc, item) => {
    acc[item['dictValue']] = item['dictLabel'];
    return acc;
  }, {});
}
export function formatNumber(value) {
  if (value === undefined || value === null) return '0';
  const formatted = parseFloat(value).toFixed(2);
  const num = formatted.replace(/\.00$/, '').replace(/(\.\d)0+$/, '$1');
  return Number(num);
}
export function upImage(e) {
  console.log(request)
  let that = this;
  let token = uni.getStorageSync('token');
  let list = [];
  // console.log(e)
  return new Promise((resolve, reject) => {
    for (let i = 0; i < e.length; i++) {
      uni.uploadFile({
        url: request.baseUrl + '/common/upload',
        filePath: e[i],
        name: 'file',
        header: {
          'Authorization': `Bearer ${token}` // 添加token到请求头
        },
        success: (res) => {
          let data = JSON.parse(res.data);
          console.log(data)
          list.push(data.url)
          if (list.length == e.length) {
            resolve(list);
          }
        },
        fail(err) {
          showToast('图片上传失败');
        }
      });
    }
  });
}

export function calculateDateRange(type) {
  const now = new Date();
  let start, end;

  switch (Number(type)) {
    case 1: // 今年
    start = new Date(now.getFullYear(), 0, 1);
    end = new Date(now.getFullYear(), 11, 31);
    break;
    case 2: // 本季度
    const quarter = Math.floor(now.getMonth() / 3);
    start = new Date(now.getFullYear(), quarter * 3, 1);
    end = new Date(now.getFullYear(), quarter * 3 + 3, 0);
    break;
    case 3: // 上季度
    const lastQuarter = (Math.floor((now.getMonth() - 1) / 3) + 4) % 4;
    start = new Date(now.getFullYear() - (lastQuarter === 3 ? 1 : 0), lastQuarter * 3, 1);
    end = new Date(now.getFullYear() - (lastQuarter === 3 ? 1 : 0), lastQuarter * 3 + 3, 0);
    break;
    case 4: // 本月
    start = new Date(now.getFullYear(), now.getMonth(), 1);
    end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
    break;
    case 5: // 上月
    start = new Date(now.getFullYear(), now.getMonth() - 1, 1);
    end = new Date(now.getFullYear(), now.getMonth(), 0);
    break;
    case 6: // 本周
    const dayOfWeek = now.getDay();
    start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - dayOfWeek);
    end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (6 - dayOfWeek));
    break;
    case 7: // 上周
    const lastDayOfWeek = now.getDay();
    start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - lastDayOfWeek - 7);
    end = new Date(now.getFullYear(), now.getMonth(), now.getDate() - lastDayOfWeek - 1);
    break;
    case 8: // 昨天
    start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
    end = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
    break;
    case 9: // 今天
    start = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    end = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    break;
    default:
    start = null;
    end = null;
  }
  // 将日期对象转换为年月日字符串格式
  return [start ? formatDate(start) : '', end ? formatDate(end) : ''];
}

function formatDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}